package longrise.phone.com.bjjt_jyb.compensate;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILFMsgListener;
import com.longrise.android.LFView;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;

import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.ImageLoaderForSettingUtils;
import uk.co.senab.photoview.PhotoView;


public class WebViewLoad extends LFView implements View.OnClickListener, ILFMsgListener {
    private Context context = null;
    private View view = null;
    private LinearLayout hn_btn_back;
    private String appcaseno;
    private Button bt_webload;
    private String kckpUserType;
    private Integer fromwhere;
    private String url;
    private PhotoView iv_one;
    private ProgressBar myProgressBar;


    public WebViewLoad(Context c, String appcaseno, String kckpUserType, String url, Integer fromwhere) {
        super(c);
        this.context = c;
        this.appcaseno = appcaseno;
        this.kckpUserType = kckpUserType;
        this.fromwhere = fromwhere;
        this.url = url;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
    }

    private void initView() {
        view = View.inflate(context, R.layout.webviewload, null);
        bt_webload = (Button) view.findViewById(R.id.bt_webload);
        hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        iv_one = (PhotoView) view.findViewById(R.id.iv_one);
        myProgressBar = (ProgressBar) view.findViewById(R.id.myProgressBar);
        tv_title.setText("认定书");
        if (fromwhere == 1) {
            bt_webload.setVisibility(View.GONE);
            hn_btn_back.setVisibility(View.VISIBLE);
        } else {
            bt_webload.setVisibility(View.VISIBLE);
            hn_btn_back.setVisibility(View.INVISIBLE);
        }
        initview();
    }


    private void initview() {
        ImageLoaderForSettingUtils.getInstance(context).display(url, iv_one, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                iv_one.setImageBitmap(bitmap);
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String s, View view, int i, int i1) {
                myProgressBar.setMax(i1);
                if (i == i1) {
                    myProgressBar.setVisibility(View.INVISIBLE);
                } else {
                    if (View.INVISIBLE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(i);
                }
            }
        });

    }

    private void regEvent(boolean b) {
        if (null != bt_webload) {
            bt_webload.setOnClickListener(b ? this : null);
        }
        if (null != hn_btn_back) {
            hn_btn_back.setOnClickListener(b ? this : null);
        }
        if (b) {
            this.addILFMsgListener(this);
        } else {
            this.removeILFMsgListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hn_btn_back:
                closeForm(false);
                OnDestroy();
                break;
            case R.id.bt_webload:
                enternext();
                break;
        }
    }

    private void enternext() {
        SingleCarOver singlecarover = new SingleCarOver(context, appcaseno);
        FrameworkManager.getInstance().showNewForm(context, singlecarover);
    }

    @Override
    public Object onLFMsg(int i, Object... objects) {
        if (i == -10) {//点击返回按键
            if (fromwhere == 1) {
                closeForm(false);
                OnDestroy();
            }
        }
        return false;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        regEvent(false);
        view = null;
        ImageLoaderForSettingUtils.getInstance(context).clearCache();
    }
}
